package com.bases {
	import com.bases.ScollBar.TBaseScrollBar;
	import com.bases.ScollBar.TTextFiledScrollBar;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

	/**
	 * 目前这个项目用到的,就是滚条的使用和添加内容的时候变到最下面的(不用系统的TextArea)
	 * @author pethan
	 *
	 */
	public class TTextArea extends TUIComponent {
		private var txt:TextField;
		private var scroolBar:TTextFiledScrollBar;
		private var textFormat:TextFormat;

		public function TTextArea(){
			super();
			txt = new TextField();
			this.addChild(txt);
			this.txt.background = true;
			this.txt.backgroundColor = 0xffffff;
			this.txt.border = true;
			this.txt.multiline = true;
			this.txt.wordWrap = true;
			this.txt.type = TextFieldType.INPUT;
			textFormat = new TextFormat();
			textFormat.rightMargin = 15;
			this.txt.defaultTextFormat = textFormat;
			scroolBar = new TTextFiledScrollBar(this.txt);
			this.addChild(scroolBar);
			this.relayout();
			scroolBar.visible = false;
		}

		override protected function relayout():void {
			super.relayout();
			this.scroolBar.setPos(this.txt.width - TBaseScrollBar.BUTTON_WIDTH - 1, 0);
			this.scroolBar.setSize(20, this.txt.height);
		}

		public function set editable(value:Boolean):void {
			if (value){
				this.txt.type = TextFieldType.INPUT;
			} else {
				this.txt.type = TextFieldType.DYNAMIC;
			}
		}

		public function set htmlText(value:String):void {
			this.txt.htmlText = value;
			this.txt.setTextFormat(textFormat);
		}

		public function set maxScroll(value:Boolean):void {
			this.txt.scrollV = this.txt.bottomScrollV;
		}

		public function get htmlText():String {
			return this.txt.htmlText;
		}

		override public function setSize(w:Number, h:Number):void {
			this.txt.width = w;
			this.txt.height = h;
			this.relayout();
		}

		public function set maxChars(value:Number):void {
			this.txt.maxChars = value;
		}

		public function get text():String {
			return this.txt.text;
		}

		public function set text(value:String):void {
			this.txt.text = value;
		}

		/**
		 * 设置字体大小和颜色
		 * @param size 字体
		 * @param color 颜色
		 *
		 */
		public function setFontSize(size:uint, color:uint = 0x000000):void {
			if (textFormat == null){
				textFormat = new TextFormat(null, size, color);
			} else {
				textFormat.size = size;
				textFormat.color = color;
			}
			setTextFormat(textFormat);
		}

		public function setTextFormat(format:TextFormat):void {
			this.txt.setTextFormat(format);
			this.txt.defaultTextFormat = format;
		}

		/**
		 *设置边框是否显示
		 * @param bool
		 *
		 */
		public function set border(bool:Boolean):void {
			this.txt.border = bool;
		}

		/**
		 *设置边框颜色
		 * @param color
		 *
		 */
		public function set borderColor(color:int):void {
			this.txt.borderColor = color;
		}

		/**
		 *设置背景是否显示
		 * @param bool
		 *
		 */
		public function set background(bool:Boolean):void {
			this.txt.background = bool;
		}

		/**
		 *设置是否是多行的
		 * @param bool
		 *
		 */
		public function set muilt(bool:Boolean):void {
			txt.multiline = bool;
		}

		/**
		 *侦听文本改变事件
		 * @param fun
		 *
		 */
		public function listenChange(fun:Function):void {
			this.txt.addEventListener(Event.CHANGE, fun);
		}
	}
}